<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Example
 *
 * This is an example of a few basic user interaction methods you could use
 * all done with a hardcoded array.
 *
 * @package		CodeIgniter
 * @subpackage	Rest Server
 * @category	Controller
 * @author		Phil Sturgeon
 * @link		http://philsturgeon.co.uk/code/
*/

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';

class item extends REST_Controller
{    
	function item_put()
    {
    	if(!$this->put('item_name') &&
    	   !$this->put('quantity_type_name') &&
    	   !$this->put('market_item_type_name'))
    	{
    		$this->response(array('status'=>"false", 'message' => 'Lost input'), 200); // 200 being the HTTP response code
       	}
    	$this->load->model('itemdao');
    	$item = $this->itemdao->create(
    			$this->put('item_name'),
    			$this->put('item_description'),
    			$this->put('item_brand_name'),
    			$this->put('quantity_type_name'),
    			$this->put('market_item_type_name'));
    	if($item != 0){
    		$this->response(array('result'=>$item,'status'=>"true"), 200); // 200 being the HTTP response code
    	} else {
    		$this->response(array('status'=>"false"), 200); // 200 being the HTTP response code
    	}
    }

	function item_delete() 
	{
		if(!$this->delete('item_id')) {
			$this->response(array('status'=>"false", 
					'message' => 'item_id not provided'), 200);
		}
		$this->load->model('itemdao');
		$success = $this->item->del(
				$this->delete('item_id'));
		
		$this->response(array('status'=> $success ? 'true' : 'false'), 200);
	}
	
	function getbyid_get()
	{
		if(!$this->get('item_id'))
		{
			$this->response(NULL, 400);
		}
		$this->load->model('itemdao');
		$item = $this->itemdao->getbyid($this->get('item_id'));
		$this->response(array('items'=>$item,'status'=> $item ? 'true' : 'false'), 200);
			}
	

	function getitems_get()
	{
		$this->load->model('itemdao');
		$items = $this->itemdao->getitems();
		$this->response(array('items'=>$items,'status'=> $items ? 'true' : 'false'), 200);	
	}
	
	function get_by_category_get()
	{
		if(!($this->get('market_id')&& $this->get('market_item_category_name'))) {
			$this->response(array('status'=>"false",
					'message' => 'market_id or/and market_item_category_name not provided'), 200);
		}
		$this->load->model('itemdao');
		$success = $this->itemdao->get_by_category($this->get('market_id'),
				 $this->get('market_item_category_name'));
		$this->response(array('items'=>$success,'status'=> $success ? 'true' : 'false'), 200);
	}
	
	function item_info_get()
	{
		if(!($this->get('market_item_id'))) {
			$this->response(array('status'=>"false",
					'message' => 'market_item_id not provided'), 200);
		}
		$this->load->model('itemdao');
		$success = $this->itemdao->get_item_info($this->get('market_item_id'));
		$this->response(array('info'=>$success,'status'=> $success ? 'true' : 'false'), 200);
	}
	
	function get_categories_in_market_get()
	{
		if(!($this->get('market_id'))) {
			$this->response(array('status'=>"false",
					'message' => 'market_id not provided'), 200);
		}
		$this->load->model('itemdao');
		$success = $this->itemdao->get_categories_in_market($this->get('market_id'));
		$this->response(array('items'=>$success,'status'=> $success ? 'true' : 'false'), 200);
	}
}